<template>
  <ContentWrap :show-border="false">
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px search-form"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="商品分类id" prop="categoryId" v-if="false">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入商品分类id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="分类名称" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入分类名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="地区id" prop="areaId" v-if="false">
        <el-input
          v-model="queryParams.areaId"
          placeholder="请输入地区id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="地区名称" prop="areaName" v-if="false">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入地区名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="语言" prop="language" v-if="false">
        <el-input
          v-model="queryParams.language"
          placeholder="请输入语言"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="样式" prop="style" v-if="false">
        <el-input
          v-model="queryParams.style"
          placeholder="请输入样式"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="评论时间显示规则" prop="timeRule" v-if="false">
        <el-input
          v-model="queryParams.timeRule"
          placeholder="请输入评论时间显示规则"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="使用权限 1仅自己 2所有人 3指定人员" prop="usePermission" v-if="false">
        <el-input
          v-model="queryParams.usePermission"
          placeholder="请输入使用权限 1仅自己 2所有人 3指定人员"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-if="false">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item class="el-btn" style="border: none !important">
        <el-button type="primary" color="#3721FF" @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
        >
        <el-button style="border: none" @click="resetQuery">
          <Icon class="mr-5px" icon="ant-design:clear-outlined" /> 重置</el-button
        >
        <!-- <el-button
          v-if="false"
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['roll:product-roll:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button> -->
        <el-button
          v-if="false"
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['roll:product-roll:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap :show-border="false">
    <el-row :gutter="10" class="mb6">
      <el-col :span="1.5" class="el-btn">
        <el-button
          type="primary"
          color="#3366ff"
          class="add-website-btn"
          @click="openForm('create')"
        >
          <Icon class="mr-2px" icon="ep:plus" />
          新增
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键id" align="center" prop="id" v-if="false" />
      <el-table-column label="商品分类id" align="center" prop="categoryId" v-if="false" />
      <el-table-column label="分类名称" align="center" prop="categoryName" />
      <el-table-column
        label="地区"
        prop="areaName"
        :filters="regionFiltersAdress"
        :filter-method="filterHandler"
      >
        <template #filter-icon>
          <Icon class="ml-2px" icon="ic:baseline-filter-alt" />
        </template>
        <!-- 自定义筛选弹窗内容 -->
        <template #filter="{ column }">
          <div class="custom-filter-panel">
            <!-- 搜索框 -->
            <el-input
              v-model="searchText"
              placeholder="搜索地区"
              clearable
              size="small"
              style="margin-bottom: 10px"
            />

            <!-- 筛选选项列表 -->
            <div style="max-height: 200px; overflow-y: auto">
              <el-checkbox-group v-model="selectedRegions">
                <el-checkbox
                  v-for="item in filteredOptions(column.filters)"
                  :key="item.value"
                  :label="item.value"
                  style="display: block; margin: 5px 0"
                >
                  {{ item.text }}
                </el-checkbox>
              </el-checkbox-group>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="地区名称" align="center" prop="areaName" v-if="false" />
      <el-table-column
        label="语言"
        prop="language"
        :filters="regionFiltersLanguage"
        :filter-method="filterHandler"
      >
        <template #filter-icon>
          <Icon class="ml-2px" icon="ic:baseline-filter-alt" />
        </template>
        <!-- 自定义筛选弹窗内容 -->
        <template #filter="{ column }">
          <div class="custom-filter-panel">
            <!-- 搜索框 -->
            <el-input
              v-model="searchText"
              placeholder="搜索地区"
              clearable
              size="small"
              style="margin-bottom: 10px"
            />

            <!-- 筛选选项列表 -->
            <div style="max-height: 200px; overflow-y: auto">
              <el-checkbox-group v-model="selectedRegions">
                <el-checkbox
                  v-for="item in filteredOptions(column.filters)"
                  :key="item.value"
                  :label="item.value"
                  style="display: block; margin: 5px 0"
                >
                  {{ item.text }}
                </el-checkbox>
              </el-checkbox-group>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="样式" align="center" prop="style" v-if="false" />
      <el-table-column label="评论时间显示规则" align="center" prop="timeRule" v-if="false" />
      <el-table-column
        v-if="false"
        label="使用权限 1仅自己 2所有人 3指定人员"
        align="center"
        prop="usePermission"
      />
      <el-table-column label="创建者" align="center" prop="createName" width="180px" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['roll:product-roll:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['roll:product-roll:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <productRollForm ref="formRef" @success="getList" />
  <!-- 新增 -->

  <RollingEffect
    ref="addRollingEffectRef"
    :show-dot="true"
    :title="selsectType == 'update' ? '编辑滚动效果' : '新增滚动效果'"
    width="40%"
    @confirm="getAddPops"
  />
  <!-- 正常删除弹窗 -->
  <ComfirmPops
    ref="deletePopsRef"
    :show-icon="true"
    width="500px"
    :show-dot="false"
    type="idTips"
    title="删除"
    :id="delSelected"
    @confirm="getDeletePops"
  >
    <template #content>
      <p>确定删除吗？</p>
    </template>
  </ComfirmPops>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import RollingEffect from './rollingEffect.vue'
import * as ProductRollApi from '@/api/mall/product/roll'
import productRollForm from './ProductRollForm.vue'
import { createRoll, getRollList, deleteRoll } from '@/api/roll/index'
import { handlegetLanguageList, handleGetGlobeState } from '@/utils/index'

/** 商品-滚动设置 列表 */
defineOptions({ name: 'ProductRoll' })
const selectedRegions = ref([])
const addPopsRef = ref()
const deletePopsRef = ref()
const addRollingEffectRef = ref()
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const delSelected = ref()
// 所有可筛选的地区选项
const regionFiltersAdress = ref([])
// 语言
const regionFiltersLanguage = ref([])
const rollingForm = ref({
  name: ''
})
const selsectType = ref('')
const rollingRules = ref({
  name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
})
const loading = ref(true) // 列表的加载中
const list = ref<[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  categoryId: undefined,
  categoryName: undefined,
  areaId: undefined,
  areaName: undefined,
  language: undefined,
  style: undefined,
  timeRule: undefined,
  usePermission: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

//
const getDeletePops = (bool, id) => {
  deleteRoll(id).then(() => {
    message.success(t('common.delSuccess'))

    getList()
  })
}
const getAddPops = () => {
  getList()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getRollList(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
// const handleAddClassification = (type) => {
//   addRollingEffectRef.value?.show()
// }
// 根据搜索词过滤选项
const filteredOptions = (filters) => {
  return filters.filter((item) => item.text.includes(searchText.value))
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
// 筛选逻辑
const filterHandler = (value: string, row: any, column: TableColumnCtx<any>) => {
  const property = column['property']
  return row[property] === value
}
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  selsectType.value = type
  // formRef.value.open(type, id)
  addRollingEffectRef.value?.show(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    deletePopsRef.value?.show(id)
    // 删除的二次确认
    // await message.delConfirm()
    // // 发起删除
    // await ProductRollApi.deleteProductRoll(id)
    // message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProductRollApi.exportProductRoll(queryParams)
    download.excel(data, '商品-滚动设置.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  regionFiltersLanguage.value = await handlegetLanguageList()
  let adress = await handleGetGlobeState()
  regionFiltersAdress.value = adress.map((item) => {
    return {
      ...item,
      text: item.label,
      value: item.label
    }
  })
  getList()
})
</script>
<style scoped>
@import '@/styles/website.scss';
</style>
